package bean;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public class AwardBean {
    public ArrayList<Award> adminArrayList;
    public HashMap<String,String> adminMap;
    public Connection connection;
    public PreparedStatement preparedStatement;
    public Statement statement;
    public ResultSet resultSet;

    public AwardBean() {
        this.adminArrayList = new ArrayList<>();
        this.adminMap = new HashMap<>();
    }

    public ArrayList<Award> getAwardArrayList() {
        return adminArrayList;
    }

    public HashMap<String, String> getAwardMap() {
        return adminMap;
    }

    public void link() {
        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            Connection connection=DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void loadAward(){
        String sql="select * from Award";
        try {
            statement=connection.createStatement();
            resultSet=statement.executeQuery(sql);
            while(resultSet.next()){
                Award admin=new Award(resultSet.getString(1),resultSet.getString(2),resultSet.getString(3));
                adminArrayList.add(admin);
                adminMap.put(resultSet.getString(3),resultSet.getString(1));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public void insertAward(Award admin){
        adminArrayList.add(admin);
        adminMap.put(admin.getId(),admin.getAthlete());
        String sql="insert into Award values(?,?,?)";
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,admin.getId());
            preparedStatement.setString(2,admin.getAthlete());
            preparedStatement.setString(3,admin.getReward());

            preparedStatement.execute();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public Award selectAward(String name,String id){
        String sql="select * from Award where name=? and id=?";
        int i=0;
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(1,id);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                Award admin = new Award(resultSet.getString(1),resultSet.getString(2),resultSet.getString(3));
                return admin;
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
}
